package matchthreegame.handlers;

import java.util.ArrayList;

import matchthreegame.extensions.MatchThreeGameZoneExtension;

import com.smartfoxserver.v2.SmartFoxServer;
import com.smartfoxserver.v2.api.ISFSGameApi;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.entities.invitation.Invitation;
import com.smartfoxserver.v2.entities.invitation.InvitationCallback;
import com.smartfoxserver.v2.entities.invitation.SFSInvitation;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class InviteBuddyToPlayGameHandler extends BaseClientRequestHandler {

	public static final String CMD_USER_BUSY     = "UB";
	public static final String USER_BUSY_MESSAGE = "UBM";

	MatchThreeGameZoneExtension zoneExtension;

	@Override
	public void handleClientRequest(final User inviter, ISFSObject params) {

		SmartFoxServer sfs = SmartFoxServer.getInstance();
		ISFSGameApi gameAPI = sfs.getAPIManager().getGameApi();
		zoneExtension = (MatchThreeGameZoneExtension)getParentExtension().getParentZone().getExtension();

		// get Users names
		final String inviterName = inviter.getName();
		final String inviteeName = params.getUtfString(zoneExtension.INVITEE_NAME);

		final User inviteeUser = getApi().getUserByName(inviteeName);

		if ( inviteeUser == null ) {
			trace("Invitee = null");
			return;
		}

		// put InvitationType as params
		SFSObject invitationParams = new SFSObject();
		invitationParams.putUtfString(zoneExtension.INVITATION_TYPE, zoneExtension.INVITATION_TYPE_GAME);

		// We set the timeout for a reply to 20 seconds 
		Invitation invitation = new SFSInvitation(inviter, inviteeUser, 20, invitationParams);

		trace( inviterName + " invited " + inviteeUser.getName() + " for Buddy Game.");

		// Send the invitation 
		gameAPI.sendInvitation(invitation, new InvitationCallback()
		{
			@Override
			public void onRefused(Invitation invObj, ISFSObject params)
			{
				// Handle the refused invitation
				trace("Game Invitation was Refused!");
			}

			@Override
			public void onExpired(Invitation invObj)
			{
				// Handle the expired invitation
				trace("Game Invitation has Expired!");
			}

			@Override
			public void onAccepted(Invitation invObj, ISFSObject params)
			{
				// Handle the accepted invitation
				trace("Game Invitation was Accepted!");
				startGame(inviter, inviteeUser);

			}
		});

	}


	private void startGame(User inviter, User invitee) {

		// If one of the users is in game, do NOT start new game, and send message to the other player.
		if ( inviter.getLastJoinedRoom().isGame() == true ) {

			ArrayList<User> recipient = new ArrayList<User>();
			recipient.add(invitee);

			SFSObject sfsMessage = new SFSObject();
			String messageString = inviter.getName() + " is playing other game.";
			sfsMessage.putUtfString(USER_BUSY_MESSAGE, messageString);

			send(CMD_USER_BUSY, sfsMessage, recipient, false);
			return;

		} else if ( invitee.getLastJoinedRoom().isGame() == true ) {

			ArrayList<User> recipient = new ArrayList<User>();
			recipient.add(inviter);

			SFSObject sfsMessage = new SFSObject();
			String messageString = invitee.getName() + " is playing other game.";
			sfsMessage.putUtfString(USER_BUSY_MESSAGE, messageString);

			send(CMD_USER_BUSY, sfsMessage, recipient, false);
			return;
		}

		// Create Room for both Users
		trace("Should create BUDDY game for: " + invitee.getName() + " and " + invitee.getName());

		ArrayList<Integer> roomPlayers = new ArrayList<Integer>();
		roomPlayers.add(inviter.getId());
		roomPlayers.add(invitee.getId());

		zoneExtension.startGame(roomPlayers);

	}

}
